<?php

namespace Admin\Controller;

class OptionsController extends BaseController {

    public function _initialize() {
        $this->module = '设置';
        $this->module_en = 'Options';
        parent::_initialize();
    }

    /**
     * 常规配置
     */
    public function general() {
        $this->assign($this->getOption('general'));
        $this->assign('cur_crumb', array(array('name' => '常规设置')));
        $this->assign('page_title', '常规设置');
        $this->display();
    }

    /**
     * 评论配置
     */
    public function comment() {
        $this->assign($this->getOption('comment'));
        $this->assign('cur_crumb', array(array('name' => '评论设置')));
        $this->assign('page_title', '评论设置');
        $this->display();
    }

    /**
     * 邮件配置
     */
    public function email() {
        $this->assign($this->getOption('email'));
        $this->assign('cur_crumb', array(array('name' => '邮件配置')));
        $this->assign('page_title', '邮件配置');
        $this->display();
    }

    /**
     * 阅读配置
     */
    public function read() {
        $this->assign($this->getOption('read'));
        $this->assign('cur_crumb', array(array('name' => '阅读配置')));
        $this->assign('page_title', '阅读配置');
        $this->display();
    }

    /**
     * 链接配置
     */
    public function link() {
        $this->assign($this->getOption('link'));
        $this->assign('cur_crumb', array(array('name' => '链接配置')));
        $this->assign('page_title', '链接配置');
        $this->display();
    }

    protected function getOption($type) {
        $options = D('Options')->getByModule($type);
        $data = array();
        foreach ($options as $v) {
            $data[$v['option_name']] = $v['option_value'];
        }
        return $data;
    }

    public function save() {
        $post = I('post.');
        $info = $this->getOption($post['module']);
        $data = array();
        foreach ($info as $k => $v) {
            $data[] = array(
                'option_name' => $k,
                'option_value' => is_array($post[$k]) ? serialize($post[$k]) : $post[$k],
                'option_module' => $post['module']
            );
        }
        D('Options')->addAll($data, array(), true);
        $this->success('保存成功');
    }

}
